package com.szpx.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.szpx.entity.CaipingoumaiEntity;
import com.szpx.service.CaipingoumaiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author lishihui
 * @date 2022/8/1 14:59
 */

@Controller
public class CaipingoumaiController {


    @Autowired
    private CaipingoumaiService caipingoumaiService;

    /*
     * TODO 此方法的用途:
     *
     * 查看用户详情
     *
     *
     * @return
     * @author
     * @since 2023/3/22 0:09
     */

    @GetMapping("/selectCById/{id}")
    public String selectCById(@PathVariable("id") Integer id, Model model) {
        CaipingoumaiEntity caipingoumaiEntity = caipingoumaiService.selectCById(id);
        if (caipingoumaiEntity != null) {
            model.addAttribute("caipingoumaiEntity", caipingoumaiEntity);
            return "Caipingoumai1/cp_detail";
        } else {
            return null;
        }
    }

    /*
     * TODO 此方法的用途:
     *
     * 删去用户
     *
     *
     * @return
     * @author
     * @since 2023/3/22 0:09
     */

    @RequestMapping("/delCaipin/{id}")
    public String delCaipin(@PathVariable("id") Integer id) {
        int num = caipingoumaiService.delCaipin(id);
        if (num > 0)
            return "redirect:/clist"; //重定向到用户列表请求
        else
            return null;
    }


    /*
     * TODO 此方法的用途:
     *
     * 去用户修改页面
     *
     *
     * @return
     * @author
     * @since 2023/3/22 0:09
     */

    @GetMapping("/toUpdateCaipin/{id}")
    public String toUpdatePage(@PathVariable("id") Integer id, Model model) {
        CaipingoumaiEntity caipingoumaiEntity = caipingoumaiService.selectCById(id);
        if (caipingoumaiEntity != null) {
            model.addAttribute("caipingoumaiEntity", caipingoumaiEntity); //将查询出来的信息 传到修改页面
            return "Caipingoumai1/cp_update"; //逻辑视图
        } else {
            return null;
        }
    }

    /*
     * TODO 此方法的用途:
     *
     * 修改用户
     *
     *
     * @return
     * @author
     * @since 2023/3/22 0:09
     */

    @RequestMapping("/updateCaipin")
    public String updateCaipin(CaipingoumaiEntity caipingoumaiEntity) {
        int num = caipingoumaiService.updateCaipin(caipingoumaiEntity);
        if (num > 0)
            return "redirect:/clist";
        else
            return null;
    }


    /*
     * TODO 此方法的用途:
     *
     * 增加用户
     *
     *
     * @return
     * @author
     * @since 2023/3/22 0:10
     */

    @RequestMapping("/addCaipin")
    public String addCaipin(CaipingoumaiEntity caipingoumaiEntity) {
        int num = caipingoumaiService.addCaipin(caipingoumaiEntity);
        if (num > 0)
            return "redirect:/clist";
        else
            return null;
    }

    /*
     * TODO 此方法的用途:
     *
     * 去往添加用户页面
     *
     *
     * @return
     * @author
     * @since 2023/3/23 17:28
     */

    @RequestMapping("/toAddCaipin")
    public String toAddCaipin(HttpSession session) {
        return "Caipingoumai1/cp_add";
    }



    /*
     * TODO 此方法的用途:
     *
     * 查询所有用户
     *
     *
     * @return
     * @author
     * @since 2023/3/22 0:10
     */

    @GetMapping("/clist")
    public String getCList(
            @RequestParam(defaultValue = "1") int pno,
            @RequestParam(defaultValue = "3") int pageSize,
            CaipingoumaiEntity caipingoumaiEntity, Model model) {
        PageHelper.startPage(pno, pageSize); //开始分页
        List<CaipingoumaiEntity> clist = caipingoumaiService.getListByCaipin(caipingoumaiEntity);
        PageInfo<CaipingoumaiEntity> page = new PageInfo<CaipingoumaiEntity>(clist);
        model.addAttribute("clist", clist); //将查询出来的数据放入model对象中
        model.addAttribute("page", page);
        model.addAttribute("name", caipingoumaiEntity.getName());
        model.addAttribute("price", caipingoumaiEntity.getPrice());
        model.addAttribute("pno", pno);
        return "Caipingoumai1/cp_clist";
    }
}
